<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\manager\AddressManager;
use app\common\manager\assistant\AssistantAccountManager;
use app\common\manager\assistant\AssistantManager;
use app\common\manager\BaseManager;
use think\Config;
use think\Exception;

/**
 * 用户地址管理
 */
class Address extends Api
{
    protected $noNeedLogin = ['getAssistantList'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 添加地址
     */
    public function addAddress()
    {
        $address = $this->request->param('address');
        $latitude = $this->request->param('latitude');
        $longitude = $this->request->param('longitude');
        $default = $this->request->param('default') ?? 0;
        try {
            $result = AddressManager::addAddress($this->user,$address,$latitude,$longitude,$default);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 编辑地址
     * @return void
     */
    public function editAddress()
    {
        $id = $this->request->param('id');
        $address = $this->request->param('address');
        $latitude = $this->request->param('latitude');
        $longitude = $this->request->param('longitude');
        $default = $this->request->param('default') ?? 0;
        try {
            $result = AddressManager::editAddress($this->user,$id,$address,$latitude,$longitude,$default);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 删除地址
     * @return void
     */
    public function deleteAddress(){
        $id = $this->request->param('id');
        try {
            $result = AddressManager::deleteAddress($this->user,$id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 获取地址列表
     * @return void
     */
    public function getAddressList()
    {
        $page = $this->request->param('page',1);
        try {
            $result = AddressManager::addressList($this->user,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 计算路费
     * @return void
     */
    public function calculateDistance()
    {
        $addressId = $this->request->param('addressId');
        $assistantId = $this->request->param('assistantId');
        try {
            $result = AddressManager::calculateDistance($this->user,$addressId,$assistantId);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }


}
